package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页查询护理项目
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param name 护理项目名称
     * @param status 状态：0-禁用，1-启用
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> findByPage(int pageNum, int pageSize, String name, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingProject> byPage = nursingProjectMapper.findByPage(name, status);
        PageResponse<NursingProjectVo> nursingProjectVos = PageResponse.of(byPage, NursingProjectVo.class);
        nursingProjectVos.getRecords().forEach(vo -> {
            vo.setCount(nursingProjectMapper.countById(vo.getId()));
        });
        return nursingProjectVos;
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto
     */
    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        // 补全基础信息
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.insert(nursingProject);

    }

    /**
     * 根据ID查询护理项目
     * @param id 护理项目id
     * @return
     */
    @Override
    public NursingProjectVo findById(Long id) {
        return nursingProjectMapper.findById(id);
    }

    /**
     * 更新护理项目
     * @param nursingProjectDto 护理项目信息
     */
    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDto,nursingProject);
        nursingProjectMapper.update(nursingProject);
    }

    /**
     * 更新护理项目状态
     * @param id 护理项目id
     * @param status 状态
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    /**
     * 根据ID删除护理项目
     *
     * @param id 护理项目id
     */
    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> findAll() {
        List<NursingProject> nursingProjects = nursingProjectMapper.findAll();
        List<NursingProjectVo> nursingProjectVos = BeanUtil.copyToList(nursingProjects, NursingProjectVo.class);
        nursingProjectVos.forEach(nursingProjectVo -> {
            nursingProjectVo.setCount(nursingProjectMapper.countById(nursingProjectVo.getId()));
        });
        return nursingProjectVos;
    }


}
